﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using CanalDeNoticiasMSDNv3.ViewModels;
using Microsoft.Phone.Tasks;
using System.Collections.ObjectModel;

namespace CanalDeNoticiasMSDNv3
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // Set the data context of the listbox control to the sample data
            DataContext = App.ViewModel;
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        // Load data for the ViewModel Items
        private void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            App.ViewModel.LoadData();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Control c = sender as Control;
            if (c != null)
            {
                ElementoEntradaVideo elemento = c.DataContext as ElementoEntradaVideo;
                if (elemento != null)
                {
                    NavigationService.Navigate(new Uri(
                        string.Format("/DetalleVideo.xaml?id={0}",
                    App.ViewModel.Videos.IndexOf(elemento)), UriKind.Relative));
                }
                else
                {
                    ElementoEntradaRss element = c.DataContext as ElementoEntradaRss;

                    if (element != null)
                    {
                        ObservableCollection<ElementoEntradaRss> coll;
                        string collName;

                        if (App.ViewModel.Noticias.Contains(element))
                        {
                            coll = App.ViewModel.Noticias;
                            collName = "Noticias";
                        }
                        else
                        {
                            coll = App.ViewModel.Articulos;
                            collName = "Articulos";
                        }


                        NavigationService.Navigate(new Uri(
                        string.Format("/RssDetailPage.xaml?id={0}&collName={1}",
                           coll.IndexOf(element), collName), UriKind.Relative));
                    }
                }
            }
        }

    }
}